package com.topnav.mc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.topnav.mc.admin.dao.mapper.PermissionMapper;
import com.topnav.mc.admin.dao.mapper.RoleMapper;
import com.topnav.mc.admin.dao.mapper.RolePermissionMapper;
import com.topnav.mc.admin.entity.*;
import com.topnav.mc.admin.entity.bean.RolePermissionBean;
import com.topnav.mc.admin.entity.param.RolePermissionParam;
import com.topnav.mc.admin.service.IRolePermissionService;
import com.topnav.mc.base.CommServiceImpl;
import com.topnav.mc.base.mc.McServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RolePermissionServiceImpl extends McServiceImpl<RolePermissionMapper, RolePermission, RolePermissionBean, RolePermissionParam> implements IRolePermissionService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public boolean beforeSave(RolePermission entity){
        entity.setUpdateTime(new Date());
        entity.setCreateTime (new Date());
        return true;
    }

    @Override
    public boolean beforeUpdate(RolePermission entity){
        entity.setUpdateTime(new Date());
        return true;

    }

    @Override
    public List<RolePermission> list(Wrapper<RolePermission> queryWrapper) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<RolePermission>();
        if (queryWrapper.getEntity() != null) {
            if (queryWrapper.getEntity().getId()!=null){
                wrapper.eq("id", queryWrapper.getEntity().getId());
            }
            if (queryWrapper.getEntity().getRoleId()!=null){
                wrapper.eq("role_id", queryWrapper.getEntity().getRoleId());
            }
            if (queryWrapper.getEntity().getPermissionId()!=null){
                wrapper.eq("permission_id", queryWrapper.getEntity().getPermissionId());
            }
        }
        return getInfo(getBaseMapper().selectList(wrapper));
    }

    private List<RolePermission> getInfo(List<RolePermission> rps){
        List<Long> ids = new ArrayList<Long>();
        /** Role */
        for(RolePermission d  : rps){
            if (d.getRoleId() != null && !ids.contains(d.getRoleId())){
                ids.add(d.getRoleId() );
            }
        }
        if (ids.size() > 0){
            List<Role>  list = roleMapper.selectBatchIds(ids);
            for (Role r : list){
                for(RolePermission origin : rps){
                    if (r.getId().equals(origin.getRoleId())){
                        origin.setRole(r);
                    }
                }
            }
        }
        /** User */
        ids.clear();
        for(RolePermission d  : rps){
            if (d.getPermissionId() != null && !ids.contains(d.getPermissionId())){
                ids.add(d.getPermissionId() );
            }
        }
        if (ids.size() > 0){
            List<Permission>  list = permissionMapper.selectBatchIds(ids);
            for (Permission p : list){
                for(RolePermission origin : rps){
                    if (p.getId().equals(origin.getPermissionId())){
                        origin.setPermission(p);
                    }
                }
            }
        }


        return rps;
    }
}
